#!/usr/bin/env python

_js_lists =  {
	"base" : [ 'liwe', 'object_enh', 'array_enh', 'string_enh', 'date_enh', 'ajax_manager', 'history', 'event_manager', 'module', 'locale', 'event', 'json', 'types', 'ga' ],
	"form" : [ '*base', 'form', 'form5', 'utils', 'validators', 'money', 'dom', 'drag' ],
	"cookie": [ 'cookie' ],
	"ext"  : [ '*base', '*form', '*cookie', 'dataset', 'paginator', 'lightbox', 'notifier', 'md5', 'fx', 'sound' ], #, 'fx/fade', 'fx/resize', 'md5' ],

	"wwl-base" : [ "wwl" ],
	"wwl-arrowbox" : [ "*wwl-scrollbox", "arrowbox/arrowbox","arrowbox/arrowbox.templates" ],
	"wwl-button" : [ 'button/button' ],
	"wwl-calendar" : [ "os3calendar/calendar.class", "os3calendar/calendar", "os3calendar/form.calendar" ],
	"wwl-calendar5" : [ "calendar/calendar", "calendar/form.calendar" ],
	"wwl-colorpicker" : [ "colorpicker/colorpicker" ],
	"wwl-dbox" : [ "dbox/dbox" ],
	"wwl-dgrid" : [ "*wwl-dbox", "dgrid/dgrid", "dgrid/dgrid.form" ],
	"wwl-grid" : [ "os3grid/os3grid" ],
	"wwl-htmled" : [ "*wwl-toolbar", "htmled/editor", "htmled/plugins/edit", 
			 "htmled/plugins/text_styles", "htmled/plugins/text_align", "htmled/plugins/image",
			 "htmled/plugins/list", "htmled/plugins/html", 
			 "htmled/wwl.htmled", "htmled/form.htmled" ],
	"wwl-jconed" : [ "*wwl-colorpicker", "jconed/jconed.core", "jconed/jconed.editor", "jconed/jconed.toolbar",
			 "jconed/jconed.hover", "jconed/jconed.config", "jconed/jconed", "jconed/form.jconed",
			 "jconed/buttons/common", 
			 "jconed/buttons/link", 
			 "jconed/buttons/style", 
			 "jconed/buttons/html", 
			 "jconed/buttons/font", 
			 "jconed/buttons/color", 
			 "jconed/buttons/image", 
			 "jconed/buttons/table",
			 "jconed/buttons/fullscreen" 
			],
	"wwl-menu"  : [ "*wwl-button", "*wwl-popbox", "*wwl-toolbar", "*wwl-popmenu", "menu/menu" ],
	"wwl-popbox"  : [ "popbox/popbox" ],
	"wwl-popmenu"  : [ "*wwl-button", "*wwl-popbox", "*wwl-toolbar", "popmenu/popmenu" ],
	"wwl-rte" : [ "rte/rte", "rte/form.rte" ],
	"wwl-scrollbox" : [ "scrollbox/scrollbox", "scrollbox/scrollbox.templates" ],
	"wwl-suggest" : [ "*wwl-popbox", "suggest/suggest" ],
	"wwl-tabs" : [ "tabs/tabs" ],
	"wwl-toolbar" : [ "*wwl-button", 'toolbar/toolbar' ],
	"wwl-tree" : [ "tree/tree" ],
	"wwl-uploader" : [ "*wwl-progressbar", "uploader/uploader", "uploader/form.uploader" ],
	"wwl-chromeframe" : [ "chromeframe/chromeframe" ],
	"wwl-accordion" : [ "accordion/accordion", "accordion/templates/accordion" ],
	"wwl-panels" : [ "panels/panels", "panels/panels.class", "panels/templates/panels" ],
	"wwl-thumbviewer" : [ "thumbviewer/thumbviewer", "thumbviewer/thumbviewer.class", "thumbviewer/templates/thumbviewer.templates" ],
	"wwl-gallery_lb" : [ "*wwl-thumbviewer", "gallery_lb/gallery_lb", "gallery_lb/gallery_lb.templates" ],
	"wwl-gallery" : [ "*wwl-arrowbox", "gallery/gallery", "gallery/gallery.templates" ], # "gallery/scrollbox.templates" ],
	"wwl-bulletbox" : [ "bulletbox/bulletbox" ],
	"wwl-progressbar" : [ "progressbar/progressbar" ],
	"wwl-graph" : [ "graph/graph", "graph/js-class", "graph/bluff-min", "graph/excanvas" ]
}

_css_lists = {
	"form": [ "css/form", "css/notifier", "css/form5" ],
	"ext" : [ "css/lightbox" ],

	"wwl-arrowbox" : [ "arrowbox/arrowbox" ],
	"wwl-button" : [ "button/button" ],
	"wwl-calendar" : [ "os3calendar/os3calendar" ],
	"wwl-calendar5" : [ "calendar/calendar" ],
	"wwl-colorpicker" : [ "colorpicker/colorpicker" ],
	"wwl-dbox" : [ "dbox/dbox" ],
	"wwl-dgrid" : [ "dgrid/dgrid" ],
	"wwl-grid" : [ "os3grid/os3grid" ],
	"wwl-htmled" : [ "*wwl-toolbar", "htmled/htmled", "htmled/plugins/edit/edit", 
			 "htmled/plugins/html/html", "htmled/plugins/list/list", 
			 "htmled/plugins/text_align/text_align", "htmled/plugins/image/image", 
			 "htmled/plugins/text_styles/text_styles" ],
	"wwl-jconed" : [ "*wwl-colorpicker", "jconed/gfx/jconed" ],
	"wwl-menu"  : [ "menu/menu" ],
	"wwl-popbox"  : [ "popbox/popbox" ],
	"wwl-rte"  : [ "rte/rte" ],
	"wwl-scrollbox"  : [ "scrollbox/scrollbox" ],
	"wwl-tabs" : [ "tabs/tabs" ], # "tabs/grey_tabs" ],
	"wwl-toolbar" : [ "button/button", "toolbar/toolbar" ],
	"wwl-tree" : [ "tree/tree" ],
	"wwl-chromeframe" : [ "chromeframe/gfx/chromeframe" ],
	"wwl-accordion" : [ "accordion/gfx/accordion" ],
	"wwl-panels" : [ "panels/gfx/panels" ],
	"wwl-thumbviewer" : [ "thumbviewer/thumbviewer" ],
	"wwl-gallery" : [ "*wwl-arrowbox", "gallery/gallery" ],
	"wwl-gallery_lb" : [ "*wwl-thumbviewer", "gallery_lb/gallery_lb" ],
	"wwl-bulletbox" : [ "bulletbox/bulletbox" ],
	"wwl-uploader" : [ "*wwl-progressbar", "uploader/gfx/uploader" ],
	"wwl-progressbar" : [ "progressbar/gfx/progressbar" ]
	}

def add ( layman, parts, jslib_base = "os3jslib" ):
	_add_part ( layman, parts, jslib_base )


def add_wwl ( layman, parts, wwl_base = 'os3wwl' ):
	layman.add_js ( "%s/%s.js" % ( wwl_base, "wwl" ) )
	_add_part ( layman, parts, wwl_base, "wwl-" )


def _add_part ( layman, parts, url_base, prefix = "" ):
	if isinstance ( parts, list ) == False: 
		parts = [ parts ]

	if layman.liwe.cfg.get ( "bootstrap.enabled", 0 ):
		form = _js_lists [ 'form' ]
		if not "form5.bootstrap" in form:
			form.append ( "form5.bootstrap" )

	for part in parts:
		
		for el in _js_lists.get ( prefix + part, [] ):
			if el [ 0 ] == "*":
				_add_part ( layman, el [ 1 : ], url_base )
			else:
				fname = _get_fname ( layman, url_base, el, "js" )
				layman.add_js ( fname ) #"%s/%s.js" % ( url_base, el % attrs ) )

		for el in _css_lists.get ( prefix + part, [] ):
			if el [ 0 ] == "*":
				_add_part ( layman, el [ 1 : ], url_base )
			else:
				fname = _get_fname ( layman, url_base, el, "css" )
				layman.add_css ( fname )

def set_mode ( layman, mode ):
	if mode in ( 'ios', 'ipad', 'iphone', 'mobile' ):
		layman._metas [ 'viewport' ] = "width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no,target-densitydpi=device-dpi"
		layman._metas [ 'apple-mobile-web-app-capable' ] = "yes"
		layman._metas [ 'apple-mobile-web-app-status-bar-style' ] = "default"

		layman._links [ "gfx/splashscreen.png" ] = { "rel" : "apple-touch-startup-image", "href" : "gfx/splashscreen.png" }
		layman._links [ "gfx/iOS-57.png" ]       = { "rel" : "apple-touch-icon", "href" : "gfx/iOS-57.png" }
		layman._links [ "gfx/iOS-72.png" ]       = { "rel" : 'apple-touch-icon', "sizes" : "72x72",  "href" : "gfx/iOS-72.png" }
		layman._links [ "gfx/iOS-142.png" ]      = { "rel" : 'apple-touch-icon', "sizes" : "114x114",  "href" : "gfx/iOS-142.png" }

def _get_fname ( layman, url_base, el, ext = "js" ):
	import os

	fname = "%s/%s" % ( url_base, el )
	tmpname = os.path.normpath ( fname )

	if layman.is_ie: 
		if os.path.exists ( "%s.ie.%s" % ( tmpname, ext ) ):
			return "%s.ie.%s" % ( fname, ext )

	return "%s.%s" % ( fname, ext )
